local wm_fenzhan = fk.CreateSkill {
  name = "wm_fenzhan",
  tags = { Skill.Compulsory, },
}


wm_fenzhan:addEffect(fk.EventPhaseStart, {
  --   anim_type = "control",
  priority = 9,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_fenzhan.name) then return end
    return target == player and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local slash
    local needname, cardmark = "slash", "@@fenzhan_slash-phase"
    for _, cid in ipairs(room.draw_pile) do
      local card = Fk:getCardById(cid)
      if card.trueName == needname then
        slash = card
        break
      end
    end
    if not slash then
      for _, cid in ipairs(room.discard_pile) do
        local card = Fk:getCardById(cid)
        if card.trueName == needname then
          slash = card
          break
        end
      end
    end
    if slash then
      room:setCardMark(slash, cardmark, 1)
      room:obtainCard(player, slash, true, fk.ReasonJustMove)
    end
  end,
})

wm_fenzhan:addEffect(fk.DamageInflicted, {
  --   anim_type = "control",
  priority = 9,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_fenzhan.name) then return end
    return data.from and data.from == player and not data.to:isNude() and data.card and
    data.card:getMark("@@fenzhan_slash-phase") == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToChooseCard(player, {
      target = data.to,
      flag = "he",
      skill_name = wm_fenzhan.name,
    })
    if cards then
      room:throwCard(cards, wm_fenzhan.name, data.to, player)
    end
  end,
})

wm_fenzhan:addEffect(fk.CardEffectCancelledOut, {
  --   anim_type = "control",
  priority = 9,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_fenzhan.name) then return end
    return data.from and data.from == player and data.card.trueName == "slash" and not player:isNude() and
    data.card:getMark("@@fenzhan_slash-phase") == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getCardIds("he")
    local throwid = table.random(cards)
    room:throwCard(throwid, wm_fenzhan.name, player, player)
  end,
})
wm_fenzhan:addEffect(fk.Death, {
  --   anim_type = "control",
  priority = 9,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_fenzhan.name) then return end
    return target ~= player and data.damage and data.damage.from and data.damage.from == player and data.damage.card and
    data.damage.card:getMark("@@fenzhan_slash-phase") == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local getcards = { 0, 0, 0 }
    for _, cid in ipairs(room.draw_pile) do
      local card = Fk:getCardById(cid)
      if getcards[1] == 0 and card.type == Card.TypeBasic and not card.is_damage_card then getcards[1] = cid end
      if getcards[2] == 0 and card.type == Card.TypeTrick and card.is_damage_card then getcards[2] = cid end
      if getcards[3] == 0 and card.type == Card.TypeEquip then getcards[3] = cid end
    end
    if getcards[1] == 0 or getcards[2] == 0 or getcards[3] == 0 then
      for _, cid in ipairs(room.discard_pile) do
        local card = Fk:getCardById(cid)
        if getcards[1] == 0 and card.type == Card.TypeBasic and not card.is_damage_card then getcards[1] = cid end
        if getcards[2] == 0 and card.type == Card.TypeTrick and card.is_damage_card then getcards[2] = cid end
        if getcards[3] == 0 and card.type == Card.TypeEquip then getcards[3] = cid end
      end
    end
    if #getcards > 0 then
      room:obtainCard(player, getcards, true, fk.ReasonJustMove)
    end
  end,
})

wm_fenzhan:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(wm_fenzhan.name) and card:getMark("@@fenzhan_slash-phase") == 1
  end
})


return wm_fenzhan
